﻿<?php
	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);

	function doAsyncMail($param=array()){

		$urlinfo = parse_url('http://10.100.160.166/sendmail.php');
	
		$host = $urlinfo['host'];
		$path = $urlinfo['path'];
		$query = isset($param)? http_build_query($param) : '';
	
		$port = 80;
		$errno = 0;
		$errstr = '';
		$timeout = 20;
	
		$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

		stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
        stream_set_timeout($fp,1);//设置超时
		
		$out = "POST ".$path." HTTP/1.1\r\n";
		$out .= "host:".$host."\r\n";
		$out .= "content-length:".strlen($query)."\r\n";
		$out .= "content-type:application/x-www-form-urlencoded\r\n";
		$out .= "connection:close\r\n\r\n";
		$out .= $query;
	
		fputs($fp, $out);
		usleep(1000); // 这一句也是关键，如果没有这延时，可能在nginx服务器上就无法执行成功
		fclose($fp);
	}

	function SendMail($title, $Message, $to = "NONE"){
		if(isset($to)){
			switch($to){
				case "TE":
					$to = "A-TFS-ENG-EE-TE@hd.tongfangpc.com";
					break;
				case "SW":
					$to = "A-TFS-SZSW@hd.tongfangpc.com;A-TFS-ENG-EE-TE@hd.tongfangpc.com";
					break;
				case "PT":
					$to = "A-TFS-SZSW@hd.tongfangpc.com;A-TFS-RD-PT-PT@hd.tongfangpc.com;A-TFS-ENG-EE-TE@hd.tongfangpc.com";
					break;
				case "MFG":
					$to = "qinglei.li@hd.tongfangpc.com;wei.hou@hd.tongfangpc.com;sword.zhu@hd.tongfangpc.com;bridge.sun@hd.tongfangpc.com;
							qianghu.yin@hd.tongfangpc.com;Guoping.Zhou@hd.tongfangpc.com;Yingjuan.wang@hd.tongfangpc.com;
							A-TFS-ENG-EE-TE@hd.tongfangpc.com";
					break;
				case "QA":
					$to = "Qiangwei.Yin@hd.tongfangpc.com;Jiao.Yin@hd.tongfangpc.com;MQA04@hd.tongfangpc.com;hao.zhang@hd.tongfangpc.com;Sherry.Liu@hd.tongfangpc.com;
							Yingjuan.wang@hd.tongfangpc.com;Guoping.Zhou@hd.tongfangpc.com;jinyan.huang@hd.tongfangpc.com;qiufang.dong@hd.tongfangpc.com;
							yinlan.shao@hd.tongfangpc.com;Xin.Li@hd.tongfangpc.com;A-TFS-ENG-EE-TE@hd.tongfangpc.com";
					//$to = "jiahui.guo@hd.tongfangpc.com";
					break;

				case "EE":
					$to = "A-TFS-ENG-EE-EE@hd.tongfangpc.com;zhiyang.zhu@hd.tongfangpc.com";
					//$to = "jiahui.guo@hd.tongfangpc.com";
					break;

				default:
					//$to = "jiahui.guo@hd.tongfangpc.com;Qiuqin.Jiang@hd.tongfangpc.com"; //sendmail测试邮件发送到的地址
					$to = "jiahui.guo@hd.tongfangpc.com"; //sendmail测试邮件发送到的地址
					break;
			}
		}

		$headers = "X-Mailer:PHP\n";
		$headers.= "MIME-Version: 1.0\n";
		$headers.= "Content-type:text/html;charset=utf-8\n";
		$headers.= "X-Priority: 1\n";
		$headers.= "From: TE_KS<TE_KS@hd.tongfangpc.com>\r\n"; 
		$message = "<html><head><meta charset='utf-8'></head><body>${Message}</body></html>"; 
		$subject = "${title}"; 

		$message=wordwrap($message, 70);

		if (mail($to, $subject, $message, $headers)) {  
			echo "邮件发送成功!";
		} else {  
			echo "邮件发送失败!";
		}
	}

	// here to check if there is a async message sent
	if(isset($aSync)){
		SendMail($title, $msg, $to);
	}
?>